package com.lczc.soa.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lczc.soa.domain.Question;
import com.lczc.soa.domain.QuestionExample;
import com.lczc.soa.domain.SearchQuestionResponse;
import com.lczc.soa.mapper.QuestionMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {


    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public int savaQuestion(Question question) {
        if (StringUtils.isBlank(question.getQuestionId())) {
            return questionMapper.insert(question);
        } else {
            return questionMapper.updateByPrimaryKeySelective(question);
        }
    }

    @Override
    public SearchQuestionResponse findAllQuestion(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List <Question> questions = questionMapper.selectByExample(new QuestionExample());
        return new SearchQuestionResponse(((Page) questions).getTotal(), questions);
    }

    @Override
    public Question findQuestionByPrimaryKey(String id) {
        return questionMapper.selectByPrimaryKey(id);
    }

    @Override
    public SearchQuestionResponse findQuestionLikeName(String keywords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List <Question> questions = questionMapper.selectByLikeName(keywords);
        return new SearchQuestionResponse(((Page) questions).getTotal(), questions);
    }

    @Override
    public List <Question> listQuestion() {

            return questionMapper.selectByExample(new QuestionExample());



    }
}
